const API= require('../../API/api.js')
const app = getApp()
import { hexMD5 } from "../../utils/MD5.js"
Page({
    data: {

    },
    onLoad: function (options) {
        console.log(options)
        var me = this;
        var redirectUrl = options.redirectUrl;
        if (redirectUrl != null && redirectUrl != '' && redirectUrl != undefined) {
            redirectUrl = redirectUrl.replace(/#/g, "?");
            redirectUrl = redirectUrl.replace(/@/g, "=");
            me.redirectUrl = redirectUrl;
        }
    },

    //用户注册的方法 
    doLogin: function (data) {
        var me = this;
        //获取参数 
        var formObject = data.detail.value;
        //获取用户名和密码
        var username = formObject.username;
        var password = formObject.password;
        //验证用户名和密码 
        if (username.length == 0 || password.length == 0) {
            wx.showToast({
                title: '用户名和密码不能为空',
                icon: "none",
                duration: 3000
            })
        }
        //如果用户名和密码验证通过 执行else         
        else {
            //debugger;
            //md5工具引入的第一种方式,直接在用的地方require 
            // var utilMd5 = require('../../utils/MD5.js');
            //var password = utilMd5.hexMD5(password);
            //md5加密引用的第二种方式  需要在顶部import 
            var password = hexMD5(password);
            wx.showLoading({
                title: '正在登录'
            })
            API.login({
                username: username,
                password: password
            }).then(res =>{
                wx.hideLoading()
                console.log(res)
                    wx.showToast({
                        title: '登录成功',
                        icon: "success",
                        duration: 3000
                    })
                    //设置全局用户缓存 
                    app.setGlobalUserInfo(res)
                    var redirectUrl = me.redirectUrl;
                    if (redirectUrl != null && redirectUrl != '' && redirectUrl != undefined) {
                        wx.redirectTo({
                            url: redirectUrl
                        })
                    } else {
                        wx.redirectTo({
                            url: '../mine/mine',
                        })
                    }
                
            }  
            )
        }
    },

    //去注册界面
    goRegistPage: function () {
        wx.redirectTo({
            url: '../userRegist/regist',
        })

    }

})